/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.*;
import database.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Muhammad Dony
 */
public class RekapAkademikAdminServlet extends HttpServlet {
    HttpSession session;
    private String action;
    AkademikDB akademikdb;
    AkademikData listAkademik;
    
    
        protected void halamanAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        AdminData admin = (AdminData) session.getAttribute("adminMasuk");
        List listidentitas = new IdentitasDB().getIdentitassingkat();
        request.setAttribute("listidentitas", listidentitas);  
        List listakademik = new AkademikDB().getakademik();
        request.setAttribute("listakademik", listakademik);      
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/view_akademikList.jsp");
        rd.forward(request, response);
    }
    
     @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // login validation start
        session = request.getSession();
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                action = request.getParameter("action");
                if (request.getParameter("action") == null) {
                    try {
                        halamanAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(RekapAkademikAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(RekapAkademikAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } else if (action.equals("view")) {
                    try {
                        halamanAkademikLengkap(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(RekapAkademikAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(RekapAkademikAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } else if (action.equals("rekap")) {
                    try {
                        RekapAkademikLengkap(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(RekapAkademikAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(RekapAkademikAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
                else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
                rd.forward(request, response);
            }
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
            rd.forward(request, response);
        }
          
  }  
    
     
     @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get session
        session = request.getSession();
        // mengambil object sesiAdmin dari session
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        // mengambil object dengan parameter action dari request
        action = request.getParameter("action");
    }
    
     public void halamanAkademikLengkap(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Rekap Akademik");
        //request.setAttribute("buttonName", "Edit");
        String ID  = request.getParameter("id");
        List listakademik = new AkademikDB().getakademikById(ID);
        request.setAttribute("listakademik", listakademik);

        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/view_akademikLengkap.jsp");
        rd.forward(request, response);
    }
     
     public void RekapAkademikLengkap(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Rekap Akademik");
        List listidentitas = new IdentitasDB().getIdentitassingkat();
        request.setAttribute("listidentitas", listidentitas);  
        List listakademik = new AkademikDB().getakademik();
        request.setAttribute("listakademik", listakademik);   

        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/excel_rekapAkademik.jsp");
        rd.forward(request, response);
    }
     
}
